package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class RenWei {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "cksid=F50123CB90224D7BB232E5C8E28C25F0; ECP_JSESSIONID=B56E347C0ACC491184D3A1E11681AC3F; JSESSIONID=CD18EACC81C1B7FE607A2200104378EC";
    public static final String ORIGIN = "http://book.sciencereading.cn";
    public static final String REFERER = "https://www.pmph.com/main/searchResults?1=1&pageNumber=32&pageSize=12&catgCode=&search=&keyWord=";
    public static final String URL_PRE = "https://www.pmph.com/main";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum =1;
        int totalPage = 47;
        int pageSize = 12;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "https://www.pmph.com/main/searchResults?1=1";
            Map<String, Object> parameter = MapUtil.newHashMap(6);
            parameter.put("pageNumber", i);
            parameter.put("catgCode", "");
            parameter.put("search", "");
            parameter.put("keyWord", "");
            parameter.put("pageSize", pageSize);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(url, headers, parameter);
            while (result.contains("页面没有找到")) {
                //睡眠2秒继续访问
                Thread.sleep(2000);
                result = HttpUtil.doPost(url, headers, parameter);
            }
            if (StrUtil.isNotBlank(result)) {
                Document htmlDoc = Jsoup.parse(result);
                Elements bookContentList = htmlDoc.select(".result_list_item");
                if (bookContentList != null && bookContentList.size() > 0) {
                    List<CxgBook> listBook = new ArrayList<>();
                    for (Element element : bookContentList) {
                        CxgBook bookEntity = new CxgBook();
                        Element imgElement = element.select("img").first();
                        if (imgElement != null) {
                            String srcStr = imgElement.attr("src");
                            String cover = srcStr;
                            bookEntity.setCover(cover);
                        }

                        Element bookNameElement = element.select(".result_txt_wrap p").first();
                        if (bookNameElement != null) {
                            String bookNameStr = bookNameElement.text();
                            bookEntity.setBookName(bookNameStr);
                        }

                        Element digUriElement = element.select(".result_list_item_xx").first();
                        if (digUriElement != null) {
                            String digStr = digUriElement.attr("onclick");
                            String pkId = getNumbers(digStr);
                            String digUri = URL_PRE+"/pdfview/"+ pkId;
                            bookEntity.setPkId(pkId);
                            bookEntity.setDigitalUri(digUri);
                        }


                        bookEntity.setIsSync(0);
                        bookEntity.setCreateTime(LocalDateTime.now());
                        bookEntity.setPressName("人民卫生出版社");
                        bookEntity.setStatus(1);
                        bookEntity.setBookIsbn("");
                        //出版社Id不明
                        bookEntity.setPressId(317);

                        listBook.add(bookEntity);
                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("--->" + i);
                }
            }
        }
    }
    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
